package com.yonyou.pmclouds.basecom.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 编码名称校验工具类
 * <br>编码要求：数字、英文和-和_
 * <br>名称要求：编码的所有字符和@和#
 */
public final class CodeNameUtils {
    private CodeNameUtils() {

    }

    public static int checkCodeStr(String code) {
        for (int i = 0; i < code.length(); i++) {
            if (!checkCodeChar(code.charAt(i))) {
                return i;
            }
        }
        return -1;
    }

    public static int checkNameStr(String name, Integer maxLength) {
        if (maxLength == null) {
            maxLength = 300;
        }
        if (StringUtils.isEmpty(name)) {
            return 1;
        }
        if (name.length() > maxLength) {
            return 1;
        }
        return -1;
    }

    public static boolean checkCodeChar(char c) {
        if (isLatin(c)) {
            return true;
        }
        if (c >= '0' && c <= '9') {
            return true;
        }
        return c == '-' || c == '_';
    }


    public static boolean isHan(char c) {
        return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
    }

    public static boolean isLatin(char c) {
        return Character.UnicodeScript.of(c) == Character.UnicodeScript.LATIN;
    }
}
